﻿/* ********************************************************************************
 * 名称：City_RepositoryService
 * 描述：City  仓储服务接口实现类（本类通过CodeSmith软件生成生成）
 * 创建人：HFL
 * 创建日期：2020-06-29 15:01:10
 * 修改人：
 * 修改日期：
 * *******************************************************************************/
using WPay.Domain.MainModule.Interface.RepositoryServices;
using WPay.Domain.Model.DTOs;
using WPay.Domain.Model.Entities;
using WPay.Infrastructure.Repository.Base;
using WPay.Infrastructure.Repository.DapperCore;
using WPay.Infrastructure.Unity;
using WPay.Domain;
using WPay.Infrastructure.EFCore;
using WPay.Domain.Model.DTOs.Parms;
using Dapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data.Entity;



namespace WPay.Infrastructure.Repository.MainModule
{
    /// <summary>
    /// City 仓储服务接口实现类
    /// </summary>
    public class City_RepositoryService: RepositoryBase, ICity_RepositoryService
    {
        /// <summary>
        /// 查询SQL语句
        /// </summary>
        private readonly string querySql = @"select ID,ProvinceId,CityName,CityCode from City where 1=1";
        /// <summary>
        /// 查询数据条数SQL语句
        /// </summary>
        private readonly string queryCountSql = @"select count(1) from City where 1=1";
        /// <summary>
        /// 新增SQL语句
        /// </summary>
        private readonly string addSql = @"insert into City(ID,ProvinceId,CityName,CityCode) values (@ID,@ProvinceId,@CityName,@CityCode)";
        /// <summary>
        /// 修改所有字段SQL语句
        /// </summary>
        private readonly string editSql = @"update City set ProvinceId=@ProvinceId,CityName=@CityName,CityCode=@CityCode where 1=1";
        /// <summary>
        /// 表主键where条件SQL语句
        /// </summary>
        private readonly string whereByPrimaryKey = @" and ID=@ID";
        
        private readonly IEfCore_RepositoryService _efCore_RepositoryService;
        private Entities db;
        public City_RepositoryService(IEfCore_RepositoryService efCore_RepositoryService)
        {
            _efCore_RepositoryService = efCore_RepositoryService;
            db = _efCore_RepositoryService.DbContext();
        }
        
        #region 由软件生成，禁止修改，若需要扩展，请在下方进行扩展
        
        /// <summary>
        /// 根据主键获取City资料信息
        /// </summary>
        /// <param name="_iD"></param>
        /// <returns>返回单个City数据实体</returns>
        public City Get(string _iD)
        {
            string sql = querySql + whereByPrimaryKey;
            DynamicParameters param = new DynamicParameters();
            param.Add("ID",_iD);
            return base.QueryBySql<City>(sql,param);
        }
        
        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_cpnID">企业代码</param>
        /// <returns>返回City数据集合</returns>
        public List<City> GetAll(string _cpnID)
        {
            return base.QueryListBySql<City>(querySql + " and CpnID=@CpnID",new {CpnID=_cpnID});
        }
        
        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <returns>返回City数据集合</returns>
        public List<City> GetAll(Hashtable _param)
        {
            //查询SQL语句组装
            string sql = querySql + base.GetQueryConditions(_param);
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
            {
                dynamicParam.Add(key, _param[key]);
            }
            
            return base.QueryListBySql<City>(sql, dynamicParam);
        }
        
        /// <summary>
        /// 按照分页条件查询数据集
        /// </summary>
        /// <param name="_param">查询条件字典，其中key必须使用字段名</param>
        /// <param name="_pageIndex">查询页码，从1开始，必传</param>
        /// <param name="_pageSize">每页查询数据条数，必传</param>
        /// <param name="_sort">排序列名，若要降序请用列名+" DESC"，必传</param>
        /// <param name="_pageCount">返回总页码数</param>
        /// <param name="_dataCount">返回总数据条数</param>
        /// <returns>返回City数据集合</returns>
        public List<City> GetByPage(Hashtable _param, int _pageIndex, int _pageSize, string _sort, out int _pageCount, out int _dataCount)
        {
            if(_pageIndex == 0 || _pageSize == 0 || string.IsNullOrWhiteSpace(_sort))
            {
                _dataCount = 0;
                _pageCount = 0;
                return null;
            }
            //封装参数
            DynamicParameters dynamicParam = new DynamicParameters();
            foreach (string key in _param.Keys)
                dynamicParam.Add(key, _param[key]);
            //组装计数sql语句
            string countSql = queryCountSql + base.GetQueryConditions(_param);
            _dataCount = base.QueryScalarBySql<Int32>(countSql,dynamicParam);//获取总数据条数
            _pageCount = (_dataCount + _pageSize -1 ) / _pageSize;//总页数 =  (数据总条数 + 每页显示条数 - 1) / 每页显示条数
            if(_dataCount == 0)
            {
                return new List<City>();
            }
            //组装分页查询语句
            string sql = querySql + base.GetQueryConditions(_param, _pageIndex, _pageSize, _sort) ;
            
            return base.QueryListBySql<City>(sql, dynamicParam);
        }
        
        /// <summary>
        /// 插入City数据
        /// </summary>
        /// <param name="_city">需要插入的City数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(City _city)
        {
            return base.ExecuteBySql(addSql, _city);
        }
        
        /// <summary>
        /// 批量插入City数据
        /// </summary>
        /// <param name="_cityList">需要插入的City数据集合</param>
        /// <returns>返回影响数据行数</returns>
        public int Add(List<City> _cityList)
        {
            return base.BulkInsert(_cityList);
        }
        
        /// <summary>
        /// 根据主键更新City数据
        /// </summary>
        /// <param name="_city">需要更新的City数据实体</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(City _city)
        {
            return base.ExecuteBySql(editSql+whereByPrimaryKey, _city);
        }
        
        /// <summary>
        /// 根据主键更新City指定字段
        /// </summary>
        /// <param name="_city">需要更新的City数据实体</param>
        /// <param name="_updateProNames">需要更新的字段名</param>
        /// <returns>返回影响数据行数</returns>
        public int Update(City _city,params string[] _updateProNames)
        {
            string sql = @"update City set ";
            //主键值在修改时，禁止为空
            if(_city.ID == default(string))
                return 0;
            sql += base.GetUpdateSet(_updateProNames);
            return base.ExecuteBySql(sql+whereByPrimaryKey, _city);
        }
        
        #endregion
        
        /*业务扩展代码*/
        #region 业务扩展代码
         public EntityList<CityDTO> GetCityLst(int page, int rows, CityParms cityParms)
        {
            Dictionary<string, string> sort = new Dictionary<string, string>() { { "ID", "DESC" } };
            var where = PredicateBuilder.True<CityDTO>();
            var query = (from a in db.City
                         select new CityDTO
                         {
                         ID=a.ID,
                         ProvinceId=a.ProvinceId,
                         CityName=a.CityName,
                         CityCode=a.CityCode,
                         }).Where(where).AsNoTracking();
            var result = new EntityList<CityDTO>();
            result = _efCore_RepositoryService.FindAllAsQuery<CityDTO>(_efCore_RepositoryService.DataSort(query, sort), page, rows);
            result.Data.ForEach(x =>
            {
               
            });
            return result;

        }
        #endregion
    }
}
